/**
 * 
 */
package cn.edu.nju.ws.sview.wyge.relations.adataset;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Calendar;

import cn.edu.nju.ws.sview.database.DBConnectionFactory;

/**
 * @author Weiyi Ge (geweiyi@gmail.com) 2012-7-23
 * 
 */
public class LikeDislike {
	public static int[] getLikeDislike(String ds_type, int ds_id)
			throws SQLException {
		int[] row = new int[2];
		Connection con = DBConnectionFactory.getConnection();
		PreparedStatement ps = con
				.prepareStatement("SELECT like_total, dislike_total "
						+ "FROM ds_prefer where ds_type = ? and ds_id = ?");
		ps.setString(1, ds_type);
		ps.setInt(2, ds_id);
		ResultSet rs = ps.executeQuery();
		if (rs.next()) {
			int like = rs.getInt(1);
			int dislike = rs.getInt(2);
			row[0] = like;
			row[1] = dislike;
		}
		rs.close();
		ps.close();
		con.close();
		return row;
	}

	public static void insertLikeDislike(String ds_type, int ds_id, int like,
			int dislike) throws SQLException {
		insertLikeDislike(ds_type, ds_id, null, like, dislike);
	}

	public static void insertLikeDislike(String ds_type, int ds_id,
			Calendar cal, int like, int dislike) throws SQLException {
		Connection con = DBConnectionFactory.getConnection();
		PreparedStatement ps = con
				.prepareStatement("insert into ds_prefer (ds_type, ds_id,"
						+ " like_total, dislike_total) values "
						+ "(?, ?, ?, ?) on duplicate key update "
						+ "like_total=like_total+values(like_total),"
						+ " dislike_total=dislike_total+values(dislike_total)");
		ps.setString(1, ds_type);
		ps.setInt(2, ds_id);
		ps.setInt(3, like);
		ps.setInt(4, dislike);
		ps.executeUpdate();
		ps.close();
		con.close();
	}
}
